// Analysis for the state size paper 
// LEC  November 17, 2020

// Input data from CShapes
cd $OUTDIR


use analysis_data, clear

// Drop gain/loss observatinos that involve birth or death
// (to avoid double counting unification and secession events) 
//replace lnetcgain=0 if f.death==1
//replace lnetcgain=0 if l.birth==1

cd $OUTPUTDIR

global TEX 1

if ($TEX) { 
	global FMAIN  "_cox_main_stata.tex, replace "
	global FEVENT "_cox_events_stata.tex, replace "
	global FEVENT2 "_cox_events2_stata.tex, replace "
	global FEVENT3 "_cox_events3_stata.tex, replace "
	global FMAINDEM  "_cox_dem_stata.tex, replace "
	global FMAINTRADE  "_cox_trade_stata.tex, replace "
	global FOLS "_ols_main.tex, replace "
	global FPOST "_cox_post45.tex, replace "
	global WIMFEIN "_cox_wimfein.tex, replace "
} 
else {
	global FMAIN "_tab.txt, replace "
	global FEVENT "_tab.txt, append "
	global FEVENT2 "_tab.txt, append "
	global FEVENT3 "_tab.txt, append "
	global FMAINDEM  "_tab.txt, append "
	global FMAINTRADE  "_tab.txt, append "
	global FOLS "_tab.txt, append "
	global FPOST "_tab.txt, append "
	global WIMFEIN "_tab.txt, append "
}

global SPELLS " spellnum "
// global SPELLS " c.spellnum##c.spellnum##c.spellnum "

xtset idc year
gen ldem = l.dem
gen larea = l.area


label variable lnetcgain "Net terr. gain, log"
label variable lef "Ethnic frac."
label variable lagfrac "Terr. frac."
label variable llage "State age, log"
label variable llelevmean "Elev. mean, log"
label variable llelevsd "Elev. SD, log"
label variable ldem "Democracy"
label variable llpop "State population, log"
label variable lltrade "Trade openness, log"
label variable lcumulsec "# past secession"
label variable lcumulethsec "# eth. secession"
label variable lcumulnethsec "# non-eth. secession"
label variable lcumulethabsorb "# unific."
label variable lcumulnethabsorb "# conquests"




gen period = int(year/10)

gen ww = 0
replace ww = 1 if year>=1915 & year <=1918
replace ww = 1 if year>=1940 & year <=1945


gen gain1 = 0
replace gain1 = 1 if netgain>0

gen loss1 = 0
replace loss1 = 1 if netgain<0


// mlogit DV
gen mgain = 0
replace mgain = 1 if lnetcgain>0
replace mgain = -1 if lnetcgain<0


gen mgain1 = 0
replace mgain1 = 1 if mgain==1

gen mloss1 = 0
replace mloss1 = 1 if mgain==-1



// continuous DV for gain and loss

gen lngain = .
replace lngain = log(netgain+1) 
replace lngain = 0 if netgain < 0 & netgain!=.

gen lnloss = .
replace lnloss = log(abs(netgain)+1) 
replace lnloss = 0 if netgain > 0 & netgain!=.

gen event = .
replace event = 0 if netgain==0
replace event = 1 if netgain!=0 & netgain!=.

btscs event year idc, gen(dur) nspline(3)
rename _spline1  espline1
rename _spline2  espline2
rename _spline3  espline3




xtset idc year

bys idc (year): gen lcumulloss = sum(l.loss1)
gen losshist1 = 0
replace losshist1 = 1 if lcumulloss > 0 & lcumulloss != .

bys idc (year): gen lcumulgain = sum(l.gain1)
gen gainhist1 = 0
replace gainhist1 = 1 if lcumulgain > 0 & lcumulgain != .

gen ethsechist1 = 0
replace ethsechist1 = 1 if lcumulethsec > 0 & lcumulethsec != .

gen unifhist1 = 0
replace unifhist1 = 1 if lcumulethabsorb > 0 & lcumulethabsorb != .

gen irrhist1 = 0
replace irrhist1 = 1 if lcumulirr > 0 & lcumulirr != .


// DEFINE SPELLS WITH CONSTANT BORDERS

xtset idc year
gen spellonset = 0
replace spellonset = 1 if l.area != l2.area & area != .
bys idc (year): gen spellnum = sum(spellonset)
gen idstr = string(idc)+"."+string(spellnum)
egen spell = group(idstr)

bys spell: egen minspellyear = min(year)
gen spellyear = year - minspellyear + 1



 
//// event models, first define dichtomous DVs

gen absorb1 = 0
replace absorb1 = 1 if absorba>0 & absorba!=.

gen absorb1b = 0
replace absorb1b = 1 if absorbb>0 & absorbb!=.

gen unif1 = 0
replace unif1 = 1 if ethabsorba>0 & ethabsorba!=.

gen sec1 = 0
replace sec1 = 1 if secessionb>0 & secessionb!=.
replace sec1 = 1 if collapseb>0 & collapseb!=.


gen ethsec1 = 0
replace ethsec1 = 1 if ethsecb>0 & ethsecb!=.
replace ethsec1 = 1 if ethcollapseb>0 & ethcollapseb!=.
 
gen transf1 = 0
replace transf1 = 1 if transfera>0 & transfera!=.

gen irr1 = 0
replace irr1 = 1 if irra>0 & irra!=. 


gen unif1b = 0
replace unif1b = 1 if ethabsorbb>0 & ethabsorbb!=.

gen irr1b = 0
replace irr1b = 1 if irrb>0 & irrb!=.


// Defining duration variables

btscs ethsec1 year idc, gen(dur_ethsec) nspline(3)
rename _spline1  spline1ethsec
rename _spline2  spline2ethsec
rename _spline3  spline3ethsec

btscs unif1 year idc, gen(dur_unif) nspline(3)
rename _spline1  spline1unif
rename _spline2  spline2unif
rename _spline3  spline3unif

btscs irr1 year idc, gen(dur_irr) nspline(3)
rename _spline1  spline1irr
rename _spline2  spline2irr
rename _spline3  spline3irr


// MAIN DURATION MODELS

// PREPARE TABLE 4: LOSSES AND GAINS, MAIN MODELS WITH TRADE AND DEMOCRACY   

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac  llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac  llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac ldem lltrade llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac  llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m4

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac  llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m5

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac ldem lltrade llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE 4:  cshpgreg_cox_main_stata.tex
esttab m1 m2 m3 m4 m5 m6 using temp.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac ldem lltrade) ///
 keep(lef lagfrac ldem lltrade) ///
 mtitles("Loss" "Loss" "Loss" "Gain" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models \label{tab:cox:trade:$PREFIX})  ///
                  scalars("strat Strata")
				  
				  
// MAIN DURATION MODELS (OLD VERSION WITHOUT TRADE AND DEMOCRACY
// USED AS TABLE A2 WITH CONTROLS!
// 

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLSS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// OUTPUT TABLE A17: main models to be run with GeoEPR Data
esttab m1 m2 m3 m4 using $PREFIX$FMAIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models\label{tab:cox:main:$PREFIX}) ///
      scalars("strat Strata")

// OUTPUT TABLE A2: cshpgreg_cox_main_full with all controls
	  esttab m1 m2 m3 m4 using cshpgreg_cox_main_full.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac llage llarea llpop llelevmean llelevsd) ///
 keep(lef lagfrac llage llarea llpop llelevmean llelevsd) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models\label{tab:cox:main:$PREFIX:full}) ///
      scalars("strat Strata")
   
// OUTPUT TABLE A16:  cshpsov_cox_main_stata.tex (TO BE RUN WITH GREGSOV DATA)
	  esttab m1 m2 m3 m4 using cshpgreg_cox_main_full.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac ) ///
 keep(lef lagfrac ) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017 without dependencies, Cox proportional hazard models\label{tab:cox:main:$PREFIX:full}) ///
      scalars("strat Strata")
	  
   
stset year, id(idc) failure(loss1==1) scale(1)
stcox lef lagfrac, nohr cluster(idc) 


// PREPARE TABLE 8: DURATION MODELING OF EVENTS
// PREPARE TABLE A18: SAME BUT TO BE LOADED WITH GEOEPR DATA
   
eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

/*  SUPPRESSED BECAUSE OF LACK OF EVENTS
stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) 
estimates store m3

stset spellyear, id(spell) failure(unif1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year)
estimates store m4
*/ 
stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE 8:  cshpgreg_cox_events_stata.tex
// OUTPUT TABLE A18:  cshp_cox_events_stata.tex
esttab m1 m2 m5 m6 using $PREFIX$FEVENT b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Eth. Secession" "Eth. Secession" "Irredentism" "Irredentism") ///
   title(Border-change events, World 1886-2017, Cox proportional hazard models\label{tab:cox:events:$PREFIX}) ///
         scalars("strat Strata")
 

// NOT USED:
// MODELING MORE EVENTS ON SIDE B 
/* 
 eststo clear   

/*
stset spellyear, id(spell) failure(unif1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) 
estimates store m1

stset spellyear, id(spell) failure(unif1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year)
estimates store m2
*/

stset spellyear, id(spell) failure(irr1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(irr1b==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m3 m4 using $PREFIX$FEVENT2 b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
   mtitles("Irredentism" "Irredentism") ///
   title(Ethnic border-change events (State B), World 1886-2017, Cox proportional hazard models \label{tab:cox:events2:$PREFIX}) ///
            scalars("strat Strata")
*/ 
 
// PREPARE TABLE A4: MODELING NON-ETHNIC AND ETHNIC EVENTS
 
eststo clear   

stset spellyear, id(spell) failure(sec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(sec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(absorb1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(absorb1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

stset spellyear, id(spell) failure(transf1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(transf1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE A4:  cshpgreg_cox_events3_stata.tex
esttab m1 m2 m3 m4 m5 m6 using $PREFIX$FEVENT3 b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
   mtitles("Secession" "Secession" "Absorption" "Absorption" "Irredentism" "Irredentism") ///
   title(All border-change events, World 1886-2017, Cox proportional hazard models \label{tab:cox:events3:$PREFIX}) ///
               scalars("strat Strata")
   
/* NOT USED   
// MAIN MODELS WITH DEMOCRACY VAR
   
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac ldem llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac ldem llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac ldem llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac ldem llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FMAINDEM b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac ldem) ///
 keep(lef lagfrac ldem) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models \label{tab:cox:dem:$PREFIX})   ///
               scalars("strat Strata")
 
 
// MAIN MODELS WITH DEM AND TRADE

eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac ldem lltrade llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac ldem lltrade llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac ldem lltrade llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) efron
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac ldem lltrade llage llarea llpop llelevmean llelevsd spellnum, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FMAINTRADE b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac ldem lltrade) ///
 keep(lef lagfrac ldem lltrade) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models \label{tab:cox:trade:$PREFIX})  ///
                  scalars("strat Strata")
   
  */
  
  
// PREPARE TABLE A6: LOSSES AND GAINS WITH OLS

eststo clear

reghdfe lnloss lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "No"
estimates store m1

reghdfe lnloss lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year idc) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "Yes"
estimates store m2

reghdfe lngain lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "No"
estimates store m3

reghdfe lngain lef lagfrac llage llarea llpop llelevmean llelevsd dur espline*, absorb(year idc) cluster(idc) 
estadd local geocontrols "Yes"
estadd local yearfe "Yes"
estadd local statefe "Yes"
estimates store m4

// OUTPUT TABLE A6:  cshpgreg_ols_main.tex
esttab m1 m2 m3 m4 using $PREFIX$FOLS b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Log Losses and gains, World 1886-2017, linear models \label{tab:cox:ols:$PREFIX}) ///
   scalars("geocontrols Controls" "yearfe Year FE" "statefe State FE")
 


 
 
 // MAIN DURATION MODELS: PRE-/POST 1945 split
 // NOT USED
 
// Make post45 var

/*
gen post45 = 0
replace post45 = 1 if year > 1945
lab var post45 "Post-45 (0/1)"
gen lef45 = post45 * lef
lab var lef45 "Ethnic frac. x post-45"
gen lagfrac45 = post45 * lagfrac
lab var lagfrac45 "Terr. frac. x post-45"
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac post45 lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac lef45 lagfrac45 llage llarea llpop llelevmean llelevsd $SPELLSS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$FPOST b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac post45 lef45 lagfrac45) ///
 keep(lef lagfrac post45 lef45 lagfrac45) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models: Pre- vs. post-1945\label{tab:cox:post45:$PREFIX}) ///
      scalars("strat Strata")
   
*/


/* NOT USED
// MAIN DURATION MODELS: NATIONALIST ORGS from Wimmer & Feinstein
 
// Make interaction vars

lab var natorgyesno "Nationalist org."
gen lefnatorg = natorgyesno * lef
lab var lefnatorg "Ethnic frac. x nat. org."
gen lagnatorg = natorgyesno * lagfrac
lab var lagnatorg "Terr. frac. x nat. org."
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd $SPELLS, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac natorgyesno lefnatorg lagnatorg llage llarea llpop llelevmean llelevsd $SPELLSS, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

esttab m1 m2 m3 m4 using $PREFIX$WIMFEIN b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac natorgyesno lefnatorg lagnatorg) ///
 keep(lef lagfrac natorgyesno lefnatorg lagnatorg) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017, Cox proportional hazard models: Nationalist Organizations\label{tab:cox:wimfein:$PREFIX}) ///
      scalars("strat Strata")
   
*/
  

// PREPARE TABLE A8: MAIN MODELS WITHOUT RUSSIA   
   
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if name!="Russia (Soviet Union)", nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if name!="Russia (Soviet Union)", nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if name!="Russia (Soviet Union)", nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLSS if name!="Russia (Soviet Union)", nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// OUTPUT TABLE A8
esttab m1 m2 m3 m4 using cshpgreg_cox_main_stata_norussia.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1886-2017 without Russia/USSR, Cox proportional hazard models\label{tab:cox:main:$PREFIX :noRussia}) ///
      scalars("strat Strata")  


// PREPARE TABLE A11: STARTING IN 1946
eststo clear

stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if year>1945, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
 
stset spellyear, id(spell) failure(loss1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if year>1945, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if year>1945, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m3

stset spellyear, id(spell) failure(gain1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLSS if year>1945, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m4

// OUTPUT TABLE A11:  cshpgreg_cox_main_stata_1945.tex
esttab m1 m2 m3 m4 using cshpgreg_cox_main_stata_1945.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
 mtitles("Loss" "Loss" "Gain" "Gain") ///
   title(Losses and gains, World 1946-2017, Cox proportional hazard models\label{tab:cox:main:cshpgreg:1945}) ///
      scalars("strat Strata")  
	

// PREPARE TABLE A12: events after 1945
eststo clear   

stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if year>1945, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m1
   
stset spellyear, id(spell) failure(ethsec1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd $SPELLS if year>1945, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m2


stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd if year>1945, nohr cluster(idc) efron 
estadd local strat "No"
estimates store m5

stset spellyear, id(spell) failure(irr1==1) scale(1)
stcox lef lagfrac llage llarea llpop llelevmean llelevsd if year>1945, nohr cluster(idc) strata(year) efron
estadd local strat "Yes"
estimates store m6

// OUTPUT TABLE A12: 
esttab m1 m2 m5 m6 using cshpgreg_cox_events_stata_1945.tex, replace b(5) se label pr2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
 order(lef lagfrac) ///
 keep(lef lagfrac) ///
  mtitles("Eth. Secession" "Eth. Secession" "Irredentism" "Irredentism") ///
   title(Border-change events, World 1946-2017, Cox proportional hazard models\label{tab:cox:main:cshpgreg:1945}) ///
         scalars("strat Strata")
